package cn.hejiang.mamis.core.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import cn.hejiang.mamis.core.dto.AmisCrudDTO;
import cn.hejiang.mamis.core.dto.OptionDTO;
import cn.hejiang.mamis.core.dto.ResultDTO;
import cn.hejiang.mamis.core.entity.SessionDetails;
import cn.hejiang.mamis.core.entity.SysPage;
import cn.hejiang.mamis.core.service.ContextService;
import cn.hejiang.mamis.core.service.SysPageService;
import cn.hejiang.mamis.core.util.EntityUtil;
import cn.hejiang.mamis.core.util.QueryUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

@RequestMapping("/mamis/sys/page")
@RestController
@Slf4j
public class SysPageController {
	@Autowired
	ContextService contextService;

	@Autowired
	SysPageService sysPageService;

	/**
	 * 获取动态 amis 页面列表
	 */
	@GetMapping("/list")
	public ResultDTO<AmisCrudDTO<SysPage>> getPageList(final HttpServletRequest request,
			@RequestParam(name = "orderBy", required = false) final String orderBy,
			@RequestParam(name = "orderDir", required = false) final String orderDir) {
		// 查询数据库
		List<SysPage> items = sysPageService.getPageList(orderBy, orderDir, contextService.getSystem().getId());

		// 构造 amis 要求的数据结构
		final AmisCrudDTO<SysPage> data = new AmisCrudDTO<>();
		data.setItems(items);

		// 返回结果集
		return ResultDTO.success(data);
	}

	@GetMapping("/{id}")
	public ResultDTO<SysPage> getPage(final HttpServletRequest request, @PathVariable("id") String id) {
		SysPage page = sysPageService.getPageById(id, contextService.getSystem().getId());
		if (page == null) {
			return ResultDTO.error("数据不存在");
		}

		return ResultDTO.success(page);
	}

	@PostMapping()
	public ResultDTO savePage(final HttpServletRequest request, @RequestBody final SysPage obj) {
		final SessionDetails session = contextService.getSession(request);

		// 清除不可更新字段
		obj.setDeleted(null);
		obj.setOwnSysId(null);
		obj.setOwnDeptId(null);
		obj.setCreatedAt(null);
		obj.setCreatedBy(null);
		obj.setUpdatedAt(null);
		obj.setUpdatedBy(session.getCurUserId());

		boolean ret;
		if (StrUtil.isNotBlank(obj.getId())) {
			// update
			UpdateWrapper<SysPage> wrapper = QueryUtil.newUpdateWrapper(obj.getId());
			ret = sysPageService.update(obj, wrapper);
		} else {
			// insert
			obj.setId(null);
			obj.setCreatedBy(session.getCurUserId());
			obj.setOwnSysId(contextService.getSystem().getId());
			ret = sysPageService.save(obj);
		}

		return ret ? ResultDTO.success() : ResultDTO.error("保存失败");
	}

	@DeleteMapping("/{id}")
	public ResultDTO delPage(final HttpServletRequest request, @PathVariable("id") String id) {
		UpdateWrapper<SysPage> wrapper = QueryUtil.newDeleteWrapper(id);
		boolean ret = sysPageService.update(null, wrapper);

		return ret ? ResultDTO.success() : ResultDTO.error("删除失败");
	}

	@GetMapping("/option/list")
	public ResultDTO<List<OptionDTO>> getPageOptionList(final HttpServletRequest request) {
		// 构建查询
		final QueryWrapper<SysPage> wrapper = QueryUtil.newQueryWrapper(SysPage.class);
		QueryUtil.withQueryCondition(wrapper, request);

		// 查询数据库
		final List<SysPage> pageList = sysPageService.getPageList("name", "asc", contextService.getSystem().getId());
		final List<OptionDTO> sysOptionList = EntityUtil.toOptionList(pageList);

		// 返回结果集
		return ResultDTO.success(sysOptionList);
	}
}
